<?php
declare (strict_types=1);
namespace app\common\controller;

use app\BaseController;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;

class Backend extends BaseController
 {
    protected $_name = '';
    protected $menuid = 0;

    public function initialize() {
        parent::initialize();
        try {
            //网站配置缓存
            $web_config =   cache('web_config');
            if(!$web_config){
                $web_config = config_lists();
                cache('web_config', $web_config);
            }
            Config::set($web_config,'web_config');
        }catch(Exception $e){

        }

        $this->_name = request()->controller();
        $this->menuid = request()->param('menuid');
        View::assign('menuid', $this->menuid);

    }

    public function index() {
        if (method_exists($this, '_other_index')) {
            $this->_other_index();
        }
        if (method_exists($this, '_before_index')) {
            $this->_before_index();
        }else{
            $this->_list($this->_mod, [], 'id', 'desc');
        }
        return view();
    }


    public function add() {
        if(request()->isPost()){
            $data = request()->post();
            if (method_exists($this, '_before_insert')) {
                $msg = $this->_before_insert($data);
                if($msg) return fail(0,$msg,'','add');
            }
            if (method_exists($this, '_handle_insert')) {
                $data = $this->_handle_insert($data);
            }
            //新增
            if (false !== $this->_mod->create($data)->getData('id')) {
                return success(1,'操作成功','');
            }else{
                return fail(0,'操作失败','');
            }
        }else{
            if (method_exists($this, '_before_add')) {
               $this->_before_add();
            }
            if(request()->isAjax()){
                $data=[1,'',view('edit')];
                return json($data);
            }else{
                return view('edit');
            }
        }
    }

    public function edit() {
        $pk = $this->_mod->getPk();
        if(request()->isPost()){
            $data = request()->post();
            if (method_exists($this, '_before_update')) {
                $msg = $this->_before_update($data);
                if($msg) return fail(0,$msg,'','edit');
            }
            if (method_exists($this, '_handle_update')) {
                $data = $this->_handle_update($data);
            }
            //新增
            if (false !== $this->_mod->update($data)->getData('id')) {
                return success(1,'操作成功','');
            }else{
                return fail(0,'操作失败','');
            }
        }else{
            $id = request()->param($pk);
            $info = $this->_mod->find($id);
            if (method_exists($this, '_before_edit')) {
                $this->_before_edit();
            }
            View::assign('info',$info);
            if(request()->isAjax()){
                $data=[1,'',view('edit')];
                return json($data);
            }else{
                return view('edit');
            }
        }
    }

    public function ajax_edit() {
        //AJAX修改数据
        $data = request()->param();
        $res = $this->_mod->where('id',$data['id'])->update([$data['field']=>$data['val']]);
        if($res !== false){
            return success(1,'操作成功','','add');
        }else{
            return fail(0,'操作失败','','add');
        }
    }

    public function delete(){
        $pk = $this->_mod->getPk();
        if ( empty(trim(request()->param($pk))) ) {
            return error(0,'请选择要操作的数据','','del');
        }
        $ids = explode(',', trim(request()->param($pk), ','));
        if (method_exists($this, '_before_delete')) {
            $msg = $this->_before_delete($ids); //目前仅有文档删除使用了
            if($msg) return fail(0,$msg,'','del');
        }
        if (false !== $this->_mod->destroy($ids)) {
            if (method_exists($this, '_after_delete')) {
                $data = $this->_after_delete($ids);
            }
            return success(1,'删除成功','','add');
        } else {
            return fail(0,'删除失败','','add');
        }
    }


    protected function _search($Fields) {
        $map =[];
        //生成查询条件
        foreach ($Fields as $key => $val) {
            if (request()->param($val)) {
                $map[$val] = request()->param($val);
            }
        }
        return $map;
    }

    protected function _list($model, $map = array(), $sort_by = '', $order_by = '', $pagesize = 20,$field_list = '*') {
        //排序
        $mod_pk = $model->getPk();
        if (request()->param("sort")) {
            $sort = trim(request()->param("sort"));
        } else if (!empty($sort_by)) {
            $sort = $sort_by;
        } else if ($this->sort) {
            $sort = $this->sort;
        } else {
            $sort = $mod_pk;
        }
        if (request()->param("order")) {
            $order = trim(request()->param("order"));
        } else if (!empty($order_by)) {
            $order = $order_by;
        } else if ($this->order) {
            $order = $this->order;
        } else {
            $order = 'DESC';
        }
        //如果需要分页
        if ($pagesize) {
            $list = $model->field($field_list)->where($map)->order($sort . ' ' . $order)->paginate([
                    'list_rows'     =>  $pagesize,
                    'query'         =>  request()->param()
                ]);
        }else{
            $list = $model->field($field_list)->where($map)->order($sort . ' ' . $order)->select();
        }
        View::assign('list', $list);
    }

}
